
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<%@ attribute name="id" required="true" rtexprvalue="true"%>
<%@ attribute name="key" %>
<%@ attribute name="cssStyle" %>
<%@ attribute name="cssClass" %>
<%@ attribute name="validate" type="Boolean" %>
<%@ attribute name="beforeSubmit" %>
<%@ attribute name="beforeBind"%>
<%@ attribute name="submitComplete" %>
<%@ attribute name="target" %>
<%@ attribute name="value" %>
<%@ attribute name="attr" %>
<%@ attribute name="name" %>
<%@ attribute name="nameKey" %>
<%@ attribute name="tabindex" %>

<c:choose>
	<c:when test="${not empty key}">
		<c:set var="value">
			<spring:message code='${key}' />
		</c:set>
	</c:when>
	<c:when test="${not empty value }">
		<c:set var="value" value="${value}" />
	</c:when>
</c:choose>


<script type="text/javascript">

$(document).ready(function() { 
    var options = { };
    	var id = '${id}';
    	
    	var target = '${target}';
    	var before = '${beforeSubmit}';
    	var after = '${submitComplete}';
    	var validate = '${validate}';
    	
    	if(target != null && target != ''){
    		options.target = '#'+target;
    	}
    	
    	if(validate == 'true'){
    		options.beforeSubmit = function(){ 
    			var validate = $('#${id}').parents('form').valid();
    			if(validate && before!= null && before != ''){
    				window[before]();
    			}
    			return validate;
    		};
    	} else if(before != null && before != ''){
    		options.beforeSubmit = window[before];
    	}
    	
    	if(after != null && after != ''){
    		options.success = window[after];
    	}
    	
    	$('#'+id).click(function(){
    		 if('${beforeBind}' != null && '${beforeBind}' != undefined && '${beforeBind}' != '' ){
   		       window['${beforeBind}']();
   		     }
    		$(this).parents('form').ajaxForm(options);
    	});
});

function validateForm(event){
	return $('#${id}').parents('form').valid();
}

</script>

<c:if test="${empty name }">
 	<c:set var="name" value="action" />
</c:if>

<c:choose>
	<c:when test="${not empty nameKey}">
		<c:set var="name">
			<spring:message code='${nameKey}' />
		</c:set>
	</c:when>
	<c:when test="${empty name }">
		<c:set var="name" value="action" />
	</c:when>
</c:choose>


<input id="${id}" type="submit" value="${value}" style="${cssStyle}" class="${cssClass}" name="${name}" ${attr} tabindex="${tabindex}"/>